from bmdjango.settings import SECRET_KEY
import jwt
import time

class MyJwt():
    def __init__(self):
        self.secret = SECRET_KEY

    # 加密
    def jwt_encode(self, payload):
        # 载荷、盐、加密方式
        return jwt.encode(payload, self.secret, algorithm='HS256')

    # 解密
    def jwt_decode(self, token):
        # token,盐、解密方式
        return jwt.decode(token, self.secret, algorithms=['HS256'])


mjwt = MyJwt()
# user = {"id":1,"name":"zhangsan","exp":int(time.time()) + 3600}
# token = mjwt.jwt_encode(user)
# print(token)
# token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwibmFtZSI6InpoYW5nc2FuIiwiZXhwIjoxNzE5OTI0OTQ5fQ.rTPpHAx-92Pz1CmoRdO-yfGlAhAfuMInju2bFi1iixs"
# data = mjwt.jwt_decode(token)
# print(data)
# {'id': 1, 'name': 'zhangsan', 'exp': 1719924949}